//
//  YYEStatusCell.swift
//  weibo14
//
//  Created by mac on 16/3/7.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit


class YYEStatusCell: UITableViewCell {

    // toolBar的顶部约束
    var toolBarTopConstraint: Constraint?
    
    // 当前视图对应的视图模型
    var statusViewModel: YYEStatusViewModel? {
        didSet {
        
            originalStatusView.statusViewModel = statusViewModel
            statusToolBar.statusViewModel = statusViewModel
            
            // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~干掉原来的约束 来新的约束
            self.toolBarTopConstraint?.uninstall()
            
            if statusViewModel?.status?.retweeted_status != nil {
                // 0. 给转发微博视图设置数据
                retweetStatusView.statusViewModel = statusViewModel
                // 1. 显示 转发微博的视图
                retweetStatusView.hidden = false
                // 2. toolBar的顶部等于转发微博的底部
                
                
//                statusToolBar.snp_updateConstraints(closure: { (make) -> Void in
//                    self.toolBarTopConstraint = make.top.equalTo(retweetStatusView.snp_bottom).constraint
                
                    statusToolBar.snp_makeConstraints(closure: { (make) -> Void in
                        
                        self.toolBarTopConstraint = make.top.equalTo(retweetStatusView.snp_bottom).constraint
                     
                })
                
                
            }else{
                // 1. 隐藏 转发微博的视图
                retweetStatusView.hidden = true
                // 2. toolBar的顶部等于原创微博的底部
                statusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarTopConstraint = make.top.equalTo(originalStatusView.snp_bottom).constraint
                })
            }
            
            
        }
    }
    // 这是接入的关键
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        selectionStyle = .None
    
        // 设置当前cell的背景颜色
        contentView.backgroundColor = UIColor(white: 239/255, alpha: 1)
        
        // 添加子控件
        contentView.addSubview(originalStatusView)
        contentView.addSubview(retweetStatusView)
        contentView.addSubview(statusToolBar)
        
        // 添加约束
        originalStatusView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(YYEStatusCellMargin)
            // 高度写死
            //            make.height.equalTo(200)
        }
        
        retweetStatusView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalStatusView.snp_bottom)
            make.leading.trailing.equalTo(originalStatusView)
        }
        
        statusToolBar.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(originalStatusView)
            self.toolBarTopConstraint = make.top.equalTo(retweetStatusView.snp_bottom).constraint
            // 因为在没有转发微博之前,toolBar的顶部是原创微博的底部
            //            make.top.equalTo(originalStatusView.snp_bottom)
            make.height.equalTo(35)
        }
        // 约束 contentView  和cell 的关系~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        // 约束当前cell的高度与statusToolBar底部相等.约束一定要加全
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(statusToolBar)
        }
    }
    
    
    // MARK: - 懒加载控件
    
    // 原创微博的视图
    private lazy var originalStatusView: YYEOriginalStatusView = YYEOriginalStatusView()
    // 底部转发评论赞的视图
    private lazy var statusToolBar: YYEStatusToolBar = YYEStatusToolBar()
    // 转发微博的视图
    private lazy var retweetStatusView: YYERetweetStatusView = YYERetweetStatusView()
}
